﻿import flash.external.*;
class v1.remote.FlashHttpRequest {
	
	var callJsFunction:String = "Vsg.FlashHttpRequest.flash_reponseHandle";
	
	var debugCallJsFunction:String = "Vsg.FlashHttpRequest.flash_debug";
	
	function FlashHttpRequest() {

	}

	/**
	 * invoke the complete handler for load complete  
	 */	
	function completeHandler(evt:Object) {

	}
	
	/**
	 * the main invoked method.
	 */
	function sendRequest(opt:Object) {
		var $sendVars:LoadVars = new LoadVars();
		var $resultVars:LoadVars = new LoadVars();

		
		try {
			var $url = opt.url;

			if (opt.params != null) {
				for (var i:String in opt.params) {
					$sendVars[i] = opt.params[i];
					trace(escape($sendVars[i]));
				}
			}

			trace($sendVars.toString());
			// --- receive the data from server ---
			$resultVars.onData = function($src:String) {
				if ($src != null) {
					var $resultObj:Object = {
						success:true,
						response:$src
					}
					ExternalInterface.call(callJsFunction, $resultObj);
				};
			}
			
	
			$sendVars.sendAndLoad($url, $resultVars, "POST");
				
		} catch (err: Error) {
			ExternalInterface.call("Vsg.FlashRemoteProxy.flash_debug", err.toString());
		} finally {

		}		
		
	}

	/**
	* send the request handle
	*/	
	function request(opt:Object):Number {
		// --- defined the url ---
		sendRequest( opt );

		var $runId:Number = 0;
			
		return $runId;
	}
}